package com.hy.mydesktop.client.component.factory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.extjs.gxt.charts.client.model.ChartModel;
import com.extjs.gxt.charts.client.model.Legend;
import com.extjs.gxt.charts.client.model.Legend.Position;
import com.extjs.gxt.charts.client.model.Text;
import com.extjs.gxt.charts.client.model.charts.PieChart;
import com.hy.mydesktop.client.component.meta.ChartModelModelEnum;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentMetaNodeModel;
/**
 * 
 * <ul>
 * <li>开发作者：冬超</li>
 * <li>设计日期：2010-8-30；时间：上午09:45:12</li>
 * <li>类型名称：ChartFactory</li>
 * <li>设计目的：</li>
 * </ul>
 * <ul>
 * <b>修订编号：</b>
 * <li>修订日期：</li>
 * <li>修订作者：</li>
 * <li>修订原因：</li>
 * <li>修订内容：</li>
 * </ul>
 */
public class ChartModelFactory {
	public static ChartModel createChartModel(
			GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {
		
		return configureChartModel(gxtComponentMetaNodeModel);

	}
	
	/**
	 * 
	 * 
	 * @param gxtComponentMetaNodeModel
	 * @return
	 */
	private static ChartModel configureChartModel(
			GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {

		
		ChartModel model = createDefaultChartModel((String)gxtComponentMetaNodeModel.get(ChartModelModelEnum.BIAOTI.name()
				.toLowerCase()),(String)gxtComponentMetaNodeModel.get(ChartModelModelEnum.BIAOTISTYLE.name()
						.toLowerCase()));
		//背景颜色
		if (gxtComponentMetaNodeModel.get(ChartModelModelEnum.BACKGROUNDCOLOUR.name()
				.toLowerCase()) != null) {
			String backgroudColor = (String) gxtComponentMetaNodeModel
					.get(ChartModelModelEnum.BACKGROUNDCOLOUR.name().toLowerCase());
			model.setBackgroundColour(backgroudColor);

		} 
		//标题
		if (gxtComponentMetaNodeModel.get(ChartModelModelEnum.TITLE
				.name().toLowerCase()) != null) {
			Text title = new Text((String)gxtComponentMetaNodeModel.get(ChartModelModelEnum.TITLE
					.name().toLowerCase()));
			model.setTitle(title);
		}
		
		if (gxtComponentMetaNodeModel.get(ChartModelModelEnum.ENABLEEVENTS
				.name().toLowerCase()) != null) {
			Boolean enableEvents = (Boolean) gxtComponentMetaNodeModel
					.get(ChartModelModelEnum.ENABLEEVENTS.name().toLowerCase());
			model.setEnableEvents(enableEvents);

		}
		//设置图例
		if (gxtComponentMetaNodeModel.get(ChartModelModelEnum.LEGEND
				.name().toLowerCase()) != null) {
			
			Legend legend=LegendFactory.createLegend(gxtComponentMetaNodeModel);
			model.setLegend(legend);
		}
		//添加示意图
		if (gxtComponentMetaNodeModel.get(ChartModelModelEnum.ADDCHARTCONFIG
				.name().toLowerCase()) != null) {
//			List<String> colour=new ArrayList<String>();
//			colour.add("#ff0000");
//			colour.add("#00aa00");
//			colour.add("#0000ff");
//			colour.add("#ff9900");
//			colour.add("#ff00ff");
//			 
//			
//			gxtComponentMetaNodeModel.set("colours", colour);
//			gxtComponentMetaNodeModel.set("addslices", "aa");
//			gxtComponentMetaNodeModel.set("alpha1", .5f);
			PieChart pie = PieChartFactory.createPieChart(gxtComponentMetaNodeModel);
			
			model.addChartConfig(pie);
		}

		return model;
	}


	private static ChartModel createDefaultChartModel(String biaoti,String biaotistyle) {

		ChartModel model = new ChartModel(biaoti,biaotistyle);
		return model;
	}
}
